package game.fingrerGuess;

public class Game {
    private DefaultGame userGame;

    private DefaultGame artificialIntelligence;

    private int gameTimes;
    private String currentWinName;
    private int equalTimes = 0;

    private boolean equals;


    public Game(DefaultGame userGame, DefaultGame artificialIntelligence) {
        this.userGame = userGame;
        this.artificialIntelligence = artificialIntelligence;
        this.gameTimes = 0;
        this.equals = false;
    }

    /**
     * @param a :代表用户输入的数字
     *          业务逻辑：用户输入的数字和电脑产生的随机数进行比较，看谁胜，胜利的规则：
     *          <p>
     *          a:  1     2    3
     *          玩家输入的猜拳： 石头  剪刀   布
     *          b: 1     2     3
     *          电脑产生的猜拳： 石头  剪刀   布
     *          <p>
     *          情况如下：
     *          玩家胜利的情况有： a=1  b=2; a = 2  b = 3; a = 3  b = 1;
     *          电脑胜利的情况有： a = 1  b = 3; a = 2  b = 1; a = 3  b = 2;
     *          平家的情况有：  a==b    equals:true
     */
    public void play(int a) {
        this.gameTimes = gameTimes + 1;
        int b = (int) (Math.random() * 3) + 1;
        // 玩家赢
        if (a == 1 && b == 2 || a == 2 && b == 3 || a == 3 && b == 1) {
            judge(1);
            //电脑赢
        } else if (a == 1 && b == 3 || a == 2 && b == 1 || a == 3 && b == 2) {
            judge(0);
            // 平家
        } else {
            judge(2);
        }
        if (this.gameTimes % 10 == 0) {
            printAllResult();
        }
        printCurrentResult(a, b);
    }

    /**
     * i=0:电脑赢
     * i=1: 玩家赢
     *
     * @param i
     */
    private void judge(int i) {
        if (i == 1) {
            int score = userGame.getScore();
            int allScore = score + 1;
            userGame.setScore(allScore);
            equals = false;
            currentWinName = userGame.getName();
        } else if (i == 0) {
            int score = artificialIntelligence.getScore();
            int allScore = score + 1;
            artificialIntelligence.setScore(allScore);
            equals = false;
            currentWinName = artificialIntelligence.getName();
        } else if (i == 2) {
            equals = true;
            equalTimes = equalTimes + 1;
        }
    }

    public void printCurrentResult(int a, int b) {
        String userName = convertNumber(a);
        String computer = convertNumber(b);
        if (equals == false) {

            System.out.println("张三出的拳是:" + userName + ",狗蛋出的拳是：" + computer + ",当前游戏的赢家是：" + currentWinName);
        } else if (equals == true) {
            System.out.println("张三出的拳是:" + userName + ",狗蛋出的拳是：" + computer + ",当前游戏为平");

        }

    }

    public void printAllResult() {
        int totalScore = artificialIntelligence.getScore() + equalTimes + userGame.getScore();

        System.out.println(userGame.getName() + "赢了次数:" + userGame.getScore() + "," + artificialIntelligence.getName() + "赢了次数：" + artificialIntelligence.getScore() + "平家次数：" + equalTimes);
        System.out.println("游戏总次数是:" + totalScore);
    }

    /**
     * @param num 游戏的输入数字：  1    2     3
     * @return 石头  剪刀   布
     * <p>
     * 当输入其他数字 默认返回：输入有误
     */
    public String convertNumber(int num) {

        if (num == 1) {
            return "石头";
        } else if (num == 2) {
            return "剪刀";

        } else if (num == 3) {
            return "布";

        }
        return "输入有误";
    }

}
